/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.search;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.cms.entity.Keywords;
import org.grayrabbit.cms.service.search.KeywordsService;
import org.grayrabbit.cms.util.lucene.WordUtil;
import org.grayrabbit.cms.vo.search.KeywordsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>KeywordsAction</b> Struts2 action�?
 * 
 * @version 1.0,创建时间:2011-10-26 13:42:09
 */
@Controller
@Scope("prototype")
public class KeywordsAction extends GenericAction<KeywordsBean> {
	private static final long serialVersionUID = -1L;
	// Service接口接入
	@Autowired
	@Qualifier("keywordsServiceImpl")
	private KeywordsService keywordsService;

	// VO对象，由前端页面数据组装
	private KeywordsBean keywords;

	/** return 前端页面数据组装VO对象,<b>KeywordsBean</b> */
	public KeywordsBean getKeywords() {
		return keywords;
	}

	public void setKeywords(KeywordsBean keywords) {
		this.keywords = keywords;
	}

	// 分页对象
	private Page<Keywords> page = new Page<Keywords>(MAX_PAGE_NUM, true);

	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<Keywords> getPage() {
		return page;
	}

	// 接收前端form,url提交参数组装成分页property
	public void setPage(Page<Keywords> page) {
		this.page = page;
	}

	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList() {
		prepareModel();
	}

	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare() {
		if (keywords == null) {
			keywords = new KeywordsBean();
		}
	}

	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法, 如：
	 * 
	 * <pre>
	 * 	<code> name="user.userName"</code>
	 * 改写成：
	 * 	<code> name="userName"</code>
	 * action实例中，会自动组成user成员变量
	 * </pre>
	 */
	public KeywordsBean getModel() {
		return keywords;
	}

	@Override
	protected void prepareModel() {
		if (keywords != null && StringUtils.isNotBlank(keywords.getKeywordsId())) {
			keywords = (KeywordsBean) dozer.map(keywordsService.getKeywords(keywords.getKeywordsId()), KeywordsBean.class);
		}

	}

	/**
	 * 删除<b>Keywords</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到keywords-list.action
	 */
	@Override
	@Action(value = "keywords-delete", results = { @Result(name = "reload", type = "redirectAction", params = { "actionName", "keywords-list" }) })
	public String delete() {
		keywordsService.delete(key);
		return RELOAD;
	}
	
	@Action(value="keywords-delete-ajax")
	public String del() {
		String text = null;
		try { 
			keywordsService.delete(key);
			text="{flag:true,msg:'删除成功'}";
		} catch (Exception e) {
			text="{flag:false,msg:'删除失败'}";
		}
		return renderText(text);
	}
	/**
	 * Keywords的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到keywords-list.jsp页面
	 */
	@Override
	@Action(value = "keywords-list")
	public String list() {
		keywordsService.list(page, keywords);
		return SUCCESS;
	}

	/**
	 * Keywords的新增与编辑的方法执行成功能 将映射到keywords-input.jsp
	 */
	@Action(value = "keywords-input")
	public String input() {
		prepareModel();
		if(keywords!=null){
			keywords.setIsEnable(true);
		}
		return SUCCESS;
	}

	/**
	 * save or update Keywords的方法，将成员property keywords转成Keywords实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到keywords-list.action
	 */
	@Override
	@Action(value = "keywords-save", results = { @Result(name = "reload", type = "redirectAction", params = { "actionName", "keywords-list" }) })
	public String save() {
		Keywords obj = (Keywords) dozer.map(keywords, Keywords.class);
		if (!StringUtils.isNotBlank(obj.getKeywordsId())) {
			obj.setKeywordsId(null);
		}
		if(obj.getIsEnable()==null){
			obj.setIsEnable(false);
		}
		keywordsService.save(obj);
		return RELOAD;
	}

	/**
	 * 获取分词、拼音、首字母
	 * 
	 * @return
	 */
	@Action(value = "keywords-getwords")
	public String getwords() {
		if (StringUtils.isNotBlank(keywords.getKeywords())) {
			String py = WordUtil.getPinyin(keywords.getKeywords());
			String szm = WordUtil.getFirstLetter(keywords.getKeywords());
			String fc = WordUtil.getUniqueSplitWord(keywords.getKeywords(),",",false);
			Map<String, String> jsonMap = new HashMap<String, String>();
			jsonMap.put("py", py);
			jsonMap.put("szm", szm);
			jsonMap.put("fc", fc);
			return renderHtml(JSONObject.fromObject(jsonMap).toString());
		}
		return "";
	}

	/**
	 * 检查关键字是否已经使用
	 * 
	 * @return
	 */
	@Action(value = "keywords-checkwords")
	public String checkwords() {
		boolean flag = keywordsService.isExistWords(keywords.getKeywordsId(), keywords.getKeywords());
		JSONObject jo = new JSONObject();
		jo.put("flag", flag);
		return renderHtml(jo.toString());
	}
	
	@Action(value="keywords-changeEnable")
	public String changeEnable() {
		Keywords obj = keywordsService.getKeywords(keywords.getKeywordsId());
		boolean flag = false;
		if(obj.getIsEnable()!=null&&obj.getIsEnable()){
			flag = obj.getIsEnable();
		}
		obj.setIsEnable(!flag);
		keywordsService.save(obj);
		return renderText(flag?"禁用成功":"启用成功");
	}	

}